<?php

class news extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('news_model');
        $this->load->model('category');
        $this->load->library('pagination');
        date_default_timezone_set('Asia/Ho_Chi_Minh');
    }

    function index($cat = 'tat-ca', $row = 0) {
        //paging
        include('paging.php');
        $config['per_page'] = 6;
        $config['base_url'] = base_url() . "tin-tuc/" . $cat . '/';

        if ($cat == 'tat-ca') {
            $lst_news = $this->news_model->get(-1, $config['per_page'], $row);
            $config['total_rows'] = $this->news_model->get_count();
        } else {
            $category = $this->category->get_by_permalink($cat);
            $data['category'] = $category;
            $cat_id = $category->id;
            $lst_news = $this->news_model->get(-1, $config['per_page'], $row, 'date', 'desc', $cat_id);
            $config['total_rows'] = $this->news_model->get_count($cat_id);
        }

        $config['cur_page'] = $row;
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();
        //data tranfer        
        $data['lst_news'] = $lst_news;

        $data['content'] = 'news_index';
        $this->load->view('template/template', $data);
    }

    function detail($permalink) {
        $news = $this->news_model->get_by_permalink($permalink);
        $data['news'] = $news;
        
        $data['content'] = 'news_detail';
        $this->load->view('template/template', $data);
    }

    function search() {
        $keyword = $this->input->get('keyword');
        $news_arr = $this->news_model->search($keyword);
        $data['keyword'] = $keyword;
        $data['news_arr'] = $news_arr;
        
        $data['content'] = 'news_search';
        $this->load->view('template/template', $data);
    }

    function recent() {
        $news_arr = $this->news_model->get(-1, 5, 0);
        $data['news_arr'] = $news_arr;
        $this->load->view('news_recent', $data);
    }

    function footer_news() {
        $news_arr = $this->news_model->get(-1, 3, 0);
        $data['news_arr'] = $news_arr;
        $this->load->view('news_footer_news', $data);
    }

}

?>
